/**
 * @copyright (c) YX Company.
 */
package com.ry.core.base.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.ry.core.base.common.Page;
import com.ry.core.base.dao.BaseDao;
import com.ry.core.base.model.SimpleModel;
import com.ry.core.exception.BaseException;
import com.ry.core.exception.BusinessException;

/**
 * 框架默认业务类，完成基础CRUD方法
 * 
 * @author lqw
 * 
 */
public abstract class BaseServiceImpl implements IBaseService {

	public static Logger logger = Logger.getLogger(BaseServiceImpl.class);

	@Autowired
	public BaseDao baseDao;

	/**
	 * 新增对象，在add被重写的时候供调用
	 */
	@Override
	public void addSimple(SimpleModel object) throws BaseException {
		try {
			baseDao.addDefault(object);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("新增数据失败.", e);
		}
	}

	/**
	 * 新增对象,供业务service重写
	 */
	@Override
	public void add(SimpleModel object) throws BaseException {
		addSimple(object);
	}

	/**
	 * 修改对象,供业务service重写
	 */
	@Override
	public SimpleModel edit(SimpleModel object) throws BaseException {
		return editSimple(object);
	}

	/**
	 * 修改对象,供业务service重写
	 */
	@Override
	public SimpleModel editObj(SimpleModel object) throws BaseException {
		try {
			return baseDao.editObj(object);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("编辑数据失败.", e);
		}
	}

	/**
	 * 修改对象，在edit被重写的时候供调用
	 * 
	 * @param object
	 * @return
	 * @throws BaseException
	 */
	@Override
	public SimpleModel editSimple(SimpleModel object) throws BaseException {
		try {
			return baseDao.editDefault(object);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("编辑数据失败.", e);
		}
	}

	/**
	 * 根据id删除，在remove被重写的时候供调用
	 * 
	 * @param id
	 * @return
	 * @throws BaseException
	 */
	@Override
	public int removeSimple(String id) throws BaseException {
		try {
			SimpleModel base = (SimpleModel) getEntity().getConstructor()
					.newInstance();
			base.setId(id);
			baseDao.removeDefault(base);
			return 1;
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("删除数据失败", e);
		}
	}

	/**
	 * 根据id删除,供业务service重写
	 */
	@Override
	public int remove(String id) throws BaseException {
		return removeSimple(id);
	}

	/**
	 * 根据id批量删除,在removeByIdArr被重写的时候供调用
	 * 
	 * @param idArr
	 * @return
	 * @throws BaseException
	 */
	@Override
	public int removeByIdArrSimple(String[] idArr) throws BaseException {
		try {
			for (String id : idArr) {
				// SimpleModel base = (SimpleModel) getEntity().getConstructor()
				// .newInstance();
				// base.setId(id);
				remove(id);
			}
			return idArr.length;
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("批量删除数据失败", e);
		}
	}

	/**
	 * 根据id批量删除,供业务service重写
	 */
	@Override
	public int removeByIdArr(String[] idArr) throws BaseException {
		return removeByIdArrSimple(idArr);
	}

	/**
	 * 根据id批量删除,在removeByIds被重写的时候供调用
	 * 
	 * @param ids
	 * @return
	 * @throws BaseException
	 */
	@Override
	public int removeByIdsSimple(String ids) throws BaseException {
		try {
			String[] idArr = ids.split(",");
			for (String id : idArr) {
				// SimpleModel base = (SimpleModel) getEntity().getConstructor()
				// .newInstance();
				// base.setId(id);
				remove(id);
			}
			return idArr.length;
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("批量删除数据失败", e);
		}
	}

	/**
	 * 根据id批量删除,供业务service重写
	 */
	@Override
	public int removeByIds(String ids) throws BaseException {
		return removeByIdsSimple(ids);
	}

	/**
	 * 获取分页数据，在getPage被重写的时候供调用
	 * 
	 * @param page
	 * @param m
	 * @return
	 * @throws BaseException
	 */
	@Override
	public Page<?> getPageSimple(Page<?> page, Map<String, Object> m)
			throws BaseException {

		try {
			int pageNum = page.getPage();
			int pageSize = page.getPageSize();
			int startNum = (pageNum - 1) * pageSize;
			m.put("startNum", startNum);
			m.put("pageSize", pageSize);
			m.put("dir", page.getDir());
			m.put("sort", page.getSort());
			List list = baseDao.findListByMapDefault(getEntity().getName(), m);
			int count = baseDao.selectCountDefault(getEntity().getName(), m);
			page.setCollection(list);
			page.setTotalSize(count);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("查找分页数据失败.", e);
		}
		return page;

	}

	/**
	 * 获取分页数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Page<?> getPage(Page<?> page, Map<String, Object> m)
			throws BaseException {
		return getPageSimple(page, m);
	}

	/**
	 * 根据条件查询，在findListByMap被重写的时候供调用
	 * 
	 * @param m
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<?> findListByMapSimple(Map<String, Object> m)
			throws BaseException {
		try {
			return baseDao.findListByMapDefault(getEntity().getName(), m);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("查找数据失败.", e);
		}
	}

	/**
	 * 根据条件查询
	 */
	@Override
	public List<?> findListByMap(Map<String, Object> m) throws BaseException {
		return findListByMapSimple(m);
	}

	/**
	 * Mybatis根据id查询,在get被重写的时候供调用
	 * 
	 * @param id
	 * @return
	 * @throws BaseException
	 */
	@Override
	public SimpleModel getSimple(String id) throws BaseException {
		try {
			return baseDao.getDefault(getEntity().getName(), id);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("获取数据失败.", e);
		}
	}

	/**
	 * Mybatis根据id查询
	 */
	@Override
	public SimpleModel get(String id) throws BaseException {
		return getSimple(id);
	}

	/**
	 * Hibernate根据id查询,在load被重写的时候供调用
	 */
	@Override
	public SimpleModel loadSimple(String id) throws BaseException {
		try {
			return baseDao.loadDefault(getEntity(), id);
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("获取数据失败.", e);
		}
	}

	/**
	 * Hibernate根据id查询
	 */
	@Override
	public SimpleModel load(String id) throws BaseException {
		return loadSimple(id);
	}

	/**
	 * 查询所有记录,在list被重写的时候供调用
	 * 
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<?> getListSimple() throws BaseException {
		try {
			return baseDao.getListDefault(getEntity().getName());
		} catch (Exception e) {
			logger.error(BaseException.toString(e));
			throw new BusinessException("获取数据失败.", e);
		}
	}

	/**
	 * 查询所有记录
	 */
	@Override
	public List<?> getList() throws BaseException {
		return getListSimple();
	}

	/**
	 * 清空hibernate缓存
	 * 
	 * @throws BaseException
	 */

	public void clearSession() throws BaseException {
		baseDao.hibernateDao.getTemplate().clear();
	}

	/**
	 * 提交session
	 * 
	 * @throws BaseException
	 */
	public void flushSession() throws BaseException {
		baseDao.hibernateDao.getTemplate().flush();
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	/**
	 * 返回当前真实实体
	 * 
	 * @return
	 */
	public abstract Class<?> getEntity();

}
